var emailReg=/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
var mtReg=/^[0-9]{6,12}$/;
var passwordReg=/^(?![^a-zA-Z]+$)(?!\D+$)(?![^_]+$).{6,18}|(?![^a-zA-Z]+$)(?!\D+$).{6,18}$/;
var passwordErrorMsg = "密码格式错误，6-18位数字字母组合，可包含下划线";
var errorNum = 0;
var captchaNum = 2

function isIdentity(type,value){
    console.log(type,value)
    var identityType = type;
  var identityNoReg = /^(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)$/
    // var identityNoReg = /^(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)$/
    if(identityType == 1){
        if(!identityNoReg.test(value)){
            return '身份证号格式错误';
        }else if(value.substr(0,6) == 330381){
            return '该证件地区禁止注册';
        }else if(!checkBirthday(value)){
            return '18岁以下用户禁止注册';
        }
    }else{
        if(value.trim().length == 0){
            return "证件号码不能为空";
        }
    }
    return true
}
//验证身份证的年龄
function checkBirthday(obj){

    var len = obj.length;
    //身份证15位时，次序为省（3位）市（3位）年（2位）月（2位）日（2位）校验位（3位），皆为数字
    if(len == '15')
    {
        var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
        var arr_data = obj.match(re_fifteen);
        var year = arr_data[2];
        var month = arr_data[3];
        var day = arr_data[4];
        var birthday = new Date('19'+year+'/'+month+'/'+day);



        return verifyBirthday('19'+year,month,day,birthday);
    }
    //身份证18位时，次序为省（3位）市（3位）年（4位）月（2位）日（2位）校验位（4位），校验位末尾可能为X
    if(len == '18')
    {
        var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|[Xx])$/;
        var arr_data = obj.match(re_eighteen);
        var year = arr_data[2];
        var month = arr_data[3];
        var day = arr_data[4];
        var birthday = new Date(year+'/'+month+'/'+day);


        return verifyBirthday(year,month,day,birthday);
    }
   // return false;
    return true
}

function verifyBirthday(year,month,day,birthday){

    var now = new Date();
    var now_year = now.getFullYear();

    //年月日是否合理
    if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day)
    {
        //判断年份的范围（年龄小于18岁（等于18岁可以注册）也不允许注册，提示“18岁以下用户禁止注册)
        var time = now_year - year;
        if(time >= 18)
        {
            return true;
        }
        return false;
    }
    return false;
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

$(function () {
    $("#captcha-item").hide()
})
//验证码


function setCaptcha(path){
    if(errorNum < captchaNum){
        errorNum++
    }else{
        errorNum++
        $("#captcha-item img").click();
        $("#captcha-item").show();
        addCookie("captcha_show", 1, 0, path);
    }
}

function checkCaptcha(formData){
    $.ajax({
        type : "post",
        url : "/common/captcha/check",
        data : {code:formData.captcha},
        dataType:"json",
        async : false,
        success : function(data){
            if(data.status != 200){
                setCaptcha()
                layer.msg("验证码错误",{icon:2})
            }else{

                signup(formData)
            }
        }
    });
}

/**
 *         * 检查字符串是否为合法手机号码         * @param {String} 字符串
 *         * @return {bool} 是否为合法手机号码         
 */
function isPhone(aPhone) {
    var bValidate = RegExp(
        /^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}$/)
        .test(aPhone);
    if (bValidate) {
        return true;
    } else
        return false;
}